import java.util.Scanner;

public class ChessGame {
	final static boolean WHITE = true;
	final static boolean BLACK = false;

	private boolean inCheckmate;
	private boolean turn;

	private ChessBoard board;

	public static void main(String[] args) {
		new ChessGame();
	}

	public ChessGame() {
		board = new ChessBoard();

		startGame();
	}

	private void startGame() {
		//Initialize Game
		inCheckmate = false;
		turn = WHITE;

		board.setupBoard();

		//Print out starting board
		System.out.println("New Game: ");
		System.out.println(board);
		
		//Initialize game variables
		Scanner scan = new Scanner(System.in);
		String move = "";
		BoardLocation start;
		BoardLocation dest;
		boolean turnOver = false;
		ChessPiece piece;
		
		//Game play loop
		while (!inCheckmate) {
			turnOver = false;

			//Print out whos turn it is
			System.out.println(((turn == WHITE) ? "White's " : "Black's ")
					+ "turn.");
			
			//Loop for one turn
			while (!turnOver) {
				//Read in move
				System.out.print("Enter move:  ");
				move = scan.nextLine();
				System.out.println();
				
				//Parse move into locations
				start = new BoardLocation(move.substring(0, 2));
				dest = new BoardLocation(move.substring(2));

				//Try and move piece
				piece = board.getPiece(start);
				turnOver = move(piece, start, dest);
				
				if(!turnOver) {
					System.out.println("Invalid Move.");
				}
			}
			
			//Print out board and end turn
			System.out.println(board);
			turn = !turn;
		}
	}
	
	//Validate and move piece
	private boolean move(ChessPiece piece, BoardLocation start, BoardLocation dest) {
		if((piece.team == turn) && piece.isValidMove(dest)) {
			board.move(piece, dest);
			
			return true;
		}
		
		return false;
	}
}
